package com.sky.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealMapper;
import com.sky.po.DishPO;
import com.sky.po.SetmealDishPO;
import com.sky.po.SetmealPO;
import com.sky.repository.SetmealDishRepository;
import com.sky.repository.SetmealRepository;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.service.SetmealDishService;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 套餐业务实现
 */
@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealRepository,SetmealPO> implements SetmealService {

    @Autowired
    private SetmealRepository setmealRepository;
    @Autowired
    @Lazy
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;

    /**
     * 根据categoryId查询套餐
     * @param setmeal
     * @return
     */
    @Cacheable(cacheNames = "cache:setmeal",key = "#setmeal.categoryId")
    @Override
    public List<SetmealPO> list(SetmealPO setmeal) {
        List<SetmealPO> list = setmealRepository.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealRepository.getDishItemBySetmealId(id);
    }

    /**
     * 新增套餐
     * @param source
     */
    @CacheEvict(cacheNames = "cache:setmeal",key = "#source.categoryId")
    @Override
    public void saveWithDish(SetmealDTO source) {
        SetmealPO setmealPO = SetmealMapper.INSTANCE.DTOConvertPO(source);
        //新增套餐
        setmealRepository.insert(setmealPO);
        //新增菜品与套餐数据
        List<SetmealDishPO> setmealDishPOS = source.getSetmealDishes();
        setmealDishPOS.forEach(s->{
            s.setSetmealId(setmealPO.getId());
        });
        //保存套餐和菜品的关联关系
        setmealDishService.insertBatch(setmealDishPOS);
    }

    /**
     * 套餐分页查询
     * @param source
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO source) {
        Page<SetmealVO> page = new Page<>(source.getPage(), source.getPageSize());
        setmealRepository.page(page,source);
        return new  PageResult(page.getTotal(),page.getRecords());
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @CacheEvict(cacheNames = "cache:setmeal",allEntries = true)
    @Override
    public void delete(List<Long> ids) {
        List<SetmealPO> setmealPOS = setmealRepository.selectBatchIds(ids);
        setmealPOS.forEach(s->{
            if(s.getStatus().equals(StatusConstant.ENABLE)){
                //起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        //删除套餐
        setmealRepository.deleteBatchIds(ids);
        //删除套餐菜品关系表中的数据
        setmealDishService.deleteBatchBySetmealId(ids);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Cacheable(cacheNames = "cache:setmeal",key = "#id")
    @Override
    public SetmealVO getById(Long id) {
        SetmealPO setmealPO = setmealRepository.selectById(id);
        SetmealVO setmealVO = SetmealMapper.INSTANCE.POConvertVO(setmealPO);
        List<SetmealDishPO> setmealDishPOS = setmealDishService.selectBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishPOS);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param source
     */
    @CacheEvict(cacheNames = "cache:setmeal",allEntries = true)
    @Override
    public void update(SetmealDTO source) {
        SetmealPO setmealPO = SetmealMapper.INSTANCE.DTOConvertPO(source);
        //修改套餐表
        setmealRepository.updateById(setmealPO);
        //删除套餐和菜品的关联关系
        setmealDishService.deleteBatchBySetmealId(Collections.singletonList(setmealPO.getId()));
        //重新插入套餐和菜品的关联关系
        List<SetmealDishPO> setmealDishPOS = source.getSetmealDishes();
        setmealDishPOS.forEach(s->{
            s.setSetmealId(setmealPO.getId());
        });
        //保存套餐和菜品的关联关系
        setmealDishService.insertBatch(setmealDishPOS);
    }

    /**
     * 套餐起售停售
     * @param status
     * @param id
     */
    @CacheEvict(cacheNames = "cache:setmeal",allEntries = true)
    @Override
    public void startOrStop(Integer status, Long id) {
        //起售套餐时,判断套餐内是否有停售的菜品
        if (status.equals(StatusConstant.ENABLE)){
            List<DishPO> dishPOS = dishService.selectBySetmealId(id);
            dishPOS.forEach(s->{
                if (s.getStatus().equals(StatusConstant.DISABLE)){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        SetmealPO setmealPO = setmealRepository.selectById(id);
        setmealPO.setStatus(status);
        setmealRepository.updateById(setmealPO);
    }
}
